package com.example.studyllm.vo.base;


import com.example.studyllm.enums.ResultEnum;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;

/**
 * 统一返回格式
 * @author wang
 * @param <T>
 */
@Slf4j
@Data
@JsonInclude(JsonInclude.Include.ALWAYS)
public final class Result<T> implements Serializable {

    private static final long serialVersionUID = -5816713866887895850L;
    /**
     * 错误码
     */

    /**
     * 错误信息
     */
    private String msg = null;


    private Integer code = ResultEnum.ERROR.getCode();
    private boolean success;



    /**
     * 返回结果实体
     */
    private T data = null;

    public Result() {
    }

    public Result(int code, String msg, T data, boolean success) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.success = success;
    }

    public static <T> Result<T> error(String msg) {
        log.debug("返回默认错误：, msg={}",  msg);
        return new Result<>(ResultEnum.ERROR.getCode(), msg, null,false);
    }

    public static <T> Result<T> error(){
        return error(ResultEnum.ERROR.getDesc());
    }

    public static <T> Result<T> error(String msg , int code){
        log.debug("返回默认错误：, msg={}",  msg);
        return new Result<>(code, msg, null,false);
    }

    public static Result<String> error(ResultEnum resultEnum) {
        log.debug("返回错误：code={}, msg={}", resultEnum.getCode(), resultEnum.getDesc());
        return new Result<>(resultEnum.getCode(), resultEnum.getDesc(), resultEnum.getDesc(),false);
    }

    public static <T> Result<T> error(ResultEnum resultEnum, String msg) {
        log.debug("返回错误：code={}, msg={}", resultEnum.getCode(), msg);
        return new Result<T>(resultEnum.getCode(), msg, null,false);
    }
    
    public static <T> Result<T> errorForData(ResultEnum resultEnum, T data) {
        log.debug("返回错误：code={}, msg={}, data={}", resultEnum.getCode(), resultEnum.getDesc(), data);
        return new Result<>(resultEnum.getCode(), resultEnum.getDesc(), data,false);
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getDesc(), data,true);
    }

    public static  <T> Result<T> success(){
        return new Result<>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getDesc(),null,true);
    }


}
